var express = require('express')
var mongoose = require('mongoose')

var session = require('express-session')
var mongoStore = require('connect-mongo')(session)
var path = require('path')
var port  = process.env.PORT || 3000
var app = express()
var morgan = require('morgan')

app.use(morgan(':method :url :status'))

mongoose.connect('mongodb://127.0.0.1/my_imovie')
app.set('views', './app/views/pages')
app.set('view engine', 'jade')
app.use(express.static(path.join(__dirname,'public')))
app.use(require('body-parser').urlencoded({extended: true}))

app.locals.moment = require('moment') //日期格式化

app.use(session({
		  secret: 'imooc',
		  store: new mongoStore({
				url: 'mongodb://127.0.0.1/my_imovie',
				collection: 'sessions',
			  }),
		  resave: false,
		  saveUninitialized: true
}))

app.use(function(req,res,next){
	var _user = req.session.user
	
	app.locals.user = _user
	
	return next()
})

if('development' === app.get('env')){
	app.set('showStackError',true)
	app.locals.pretty = true
}


require('./config/routes')(app)

app.listen(port)
console.log('app start on port ' + port)

